<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// |畅捷支付控制器
// +----------------------------------------------------------------------
// | Author: july <1499801239@qq.com>
// +----------------------------------------------------------------------
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
class ChangPay
{
    public function bank1($data){
        $postData = array();	
		// 基本参数
        $postData['Service'] = 'nmg_biz_api_auth_req';
        $postData['Version'] = '1.0';
        $postData['PartnerId'] = $data['PartnerId'];//商户号
//      $postData['PartnerId'] = '200002080023';//商户号
		$postData['InputCharset'] = 'UTF-8';
        $postData['TradeDate'] = date('Ymd').'';
        $postData['TradeTime'] = date('His').'';
		$postData['ReturnUrl'] = 'http://dev.chanpay.com/receive.php';// 前台跳转url
		$postData['Memo'] = '备注';
		// 4.4.2.1. 鉴权绑卡请求（API） 业务参数
        $postData['TrxId'] = date('Ymd') . rand(1, 9) . date('His') . rand(111, 999); //外部流水号
		$postData['MerchantNo'] =$data['PartnerId']; //商户编号，与支付请求接口中SellerId保持一致
		$postData['ExpiredTime'] =   '90m'; //订单有效期，取值范围：1m～48h。单位为分，如1.5h，可转换为90m。用来标识本次鉴权订单有效时间，超过该期限则该笔订单作废
		$postData['MerUserId'] =   $data['MerUserId']; //用户标识
		$postData['BkAcctTp'] =   '01'; //卡类型（00 –银行贷记账户;01 –银行借记账户;）
		$postData['BkAcctNo'] =   $this->rsaEncrypt($data['BkAcctNo']); //卡号
		$postData['IDTp'] =   '01'; //证件类型，01：身份证
		$postData['IDNo'] =   $this->rsaEncrypt($data['IDNo']); //证件号
		$postData['CstmrNm'] =   $this->rsaEncrypt($data['CstmrNm']); //持卡人姓名
		$postData['MobNo'] =   $this->rsaEncrypt($data['MobNo']); //银行预留手机号		
		$postData['CardCvn2'] =   $this->rsaEncrypt(''); //CVV2码，当卡类型为信用卡时必填
		$postData['CardExprDt'] =   $this->rsaEncrypt('08/19'); //有效期，当卡类型为信用卡时必填
		$postData['SmsFlag'] =   '1'; //短信发送标识，0：不发送短信1：发送短信
		$postData['NotifyUrl'] = '';//异步通知地址
		$postData['Extension'] = '';//扩展字段
        $postData['Sign'] = $this->rsaSign($postData);
	    $postData['SignType'] = 'RSA'; //签名类型	
		$query = http_build_query($postData);
		$url = 'https://pay.chanpay.com/mag-unify/gateway/receiveOrder.do?'. $query; //该url为生产环境url
		$jsonstr = $this->httpGet_a($url);
		$json = json_decode($jsonstr);

//		
//		Cache::set('name',$value,3600);
	    return $json;
		
    }
public function bank2($data){
        $postData = array();	
		// 基本参数
        $postData['Service'] = 'nmg_api_auth_sms';
        $postData['Version'] = '1.0';
        $postData['PartnerId'] = $data['PartnerId'];//商户号
		$postData['InputCharset'] = 'UTF-8';
        $postData['TradeDate'] = date('Ymd').'';
        $postData['TradeTime'] = date('His').'';
		$postData['ReturnUrl'] = 'http://dev.chanpay.com/receive.php';// 前台跳转url
		$postData['Memo'] = '备注';
		// 4.4.2.3. 鉴权绑卡确认接口（API） 业务参数
        $postData['TrxId'] = date('Ymd') . rand(1, 9) . date('His') . rand(111, 999); //外部流水号
//      $postData['TrxId'] = '1548922432'; //外部流水号
		$postData['OriAuthTrxId'] =$data['OriAuthTrxId']; //填写需要退款的原始订单号
		$postData['SmsCode'] =  $data['SmsCode']; //鉴权短信验证码
		$postData['Extension'] = '';//扩展字段
        $postData['Sign'] = $this->rsaSign($postData);
	    $postData['SignType'] = 'RSA'; //签名类型
		$query = http_build_query($postData);
		$url = 'https://pay.chanpay.com/mag-unify/gateway/receiveOrder.do?'. $query; //该url为生产环境url
		$jsonstr = $this->httpGet_a($url);
		$json = json_decode($jsonstr);
	    return $json;
    }

    public function rsaSign($args) {
	    $args=array_filter($args);//过滤掉空值
	    ksort($args);
	    $query  =   '';
	    foreach($args as $k=>$v){
	        if($k=='SignType'){
	            continue;
	        }
	        if($query){
	            $query  .=  '&'.$k.'='.$v;
	        }else{
	            $query  =  $k.'='.$v;
	        }
	    }
	    //这地方不能用 http_build_query  否则会urlencode
	    //$query=http_build_query($args);
        $path   =   "./rsa_private_key.pem";  //私钥地址
//      $re=file_exists($path);
	    $private_key= file_get_contents($path);
	    $pkeyid = openssl_get_privatekey($private_key);
	    openssl_sign($query, $sign, $pkeyid);
	    openssl_free_key($pkeyid);
	    $sign = base64_encode($sign);
	    return $sign;
	}

    public function rsaEncrypt($content){	
    	$public_key_path   =   "./rsa_public_key.pem";  //公钥地址 
        $pubKey = file_get_contents($public_key_path);
        $res = openssl_get_publickey($pubKey);
        //把需要加密的内容，按128位拆开解密
        $result  = '';
        for($i = 0; $i < strlen($content)/128; $i++  ) {
            $data = substr($content, $i * 128, 128);
            openssl_public_encrypt ($data, $encrypt, $res);
            $result .= $encrypt;
        }
        $result = base64_encode($result);
        openssl_free_key($res);
        return $result;
    }
    public function httpGet_a($order_url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $order_url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json = curl_exec($ch);
        curl_close($ch);
        return $json;
	}
	
	//用户支付

}





















?>